package com.rohoo.framework.struts.action;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.rohoo.framework.bo.BaseObject;
import com.rohoo.framework.dao.DataPackage;
import com.wondersgroup.sse.app.Global;
import com.wondersgroup.sse.app.comm.UserProfile;
import com.wondersgroup.sse.app.comm.UserProfileImpl;
import com.wondersgroup.sse.uams.model.Org;

public class BaseAction extends ActionSupport implements Action {

	public Log log = LogFactory.getLog(this.getClass());

	private BaseObject bo = null;

	private DataPackage datas = null;

	private int _currpage = 1;

	private int _pagelines = 10;

	private String _orderby = "";

	private String _orderby1 = "";

	private int _rowcount = 0;

	private int _selectpage = 0;

	@SuppressWarnings("unchecked")
	private static final ThreadLocal _params = new ThreadLocal();

	private static final long serialVersionUID = 6363182240218526364L;

	private static final int BUFFER_SIZE = 16 * 1024;

	private String name;

	private String contentType;

	private String fileName;

	private String inputPath;

	private String url = "";

	private UserProfile userProfile;

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		try {
			this.fileName = new String(fileName.getBytes(), "ISO8859-1");
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public String getInputPath() {
		return inputPath;
	}

	public void setInputPath(String value) throws UnsupportedEncodingException {
		this.inputPath = value;
	}

	public InputStream getInputStream() throws Exception {
		return new java.io.FileInputStream(inputPath);
		// return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
	}

	public void doSysDownload() throws Exception {
		// TODO:
		throw new Exception();
	}

	public void setBo(BaseObject bo) {
		this.bo = bo;
	}

	public BaseObject getBo() {
		return bo;
	}

	public DataPackage getDatas() {
		return datas;
	}

	public void setDatas(DataPackage datas) {
		this.datas = datas;
	}

	public int get_currpage() {
		return _currpage;
	}

	public void set_currpage(int _currpage) {
		this._currpage = _currpage;
	}

	public int get_pagelines() {
		return _pagelines;
	}

	public void set_pagelines(int _pagelines) {
		this._pagelines = _pagelines;
	}

	public String get_orderby() {
		return _orderby;
	}

	public void set_orderby(String _orderby) {
		this._orderby = _orderby;
	}

	public String get_orderby1() {
		return _orderby1;
	}

	public void set_orderby1(String _orderby1) {
		this._orderby1 = _orderby1;
	}

	public int get_rowcount() {
		return _rowcount;
	}

	public void set_rowcount(int _rowcount) {
		this._rowcount = _rowcount;
	}

	public int get_selectpage() {
		return _selectpage;
	}

	public void set_selectpage(int _selectpage) {
		this._selectpage = _selectpage;
	}

	public UserProfile getUserProfile() {
		UserProfile userProfile = new UserProfileImpl();
		
		if(getRequest().getSession().getAttribute(Global.USER_PROFILE_KEY)== null){
			return userProfile;
		}else{
			userProfile = (UserProfileImpl)getRequest().getSession().getAttribute(Global.USER_PROFILE_KEY);
		}
		return userProfile;
	}
	
	public Org getUserOrg(){
		
		Org org = new Org();
		
		if(getRequest().getSession().getAttribute("SSE_AppLoginUser.Org")== null){
			return org;
		}else{
			org = (Org)getRequest().getSession().getAttribute("SSE_AppLoginUser.Org");
		}
		return org;
	}
	
	public String getDwdm(){
		return getUserOrg().getDwdm();
	}
	public String getUserOrgId(){
		return getUserOrg().getOrgid();
	}

	public void setUserProfile(UserProfile userProfile) {
		this.userProfile = userProfile;
	}

	public static ActionContext getContext() {
		ActionContext context = ActionContext.getContext();
		return context;
	}

	public static HttpServletRequest getRequest() {
		return (HttpServletRequest) getContext().get(ServletActionContext.HTTP_REQUEST);
	}

	public static HttpServletResponse getResponse() {
		return (HttpServletResponse) getContext().get(ServletActionContext.HTTP_RESPONSE);
	}

	public static HttpSession getSession() {
		return getRequest().getSession();
	}

	@SuppressWarnings("unchecked")
	public static Map getParameters() {
		return getContext().getParameters();
	}

	@SuppressWarnings("unchecked")
	public ParamsTable getParams() {
		ParamsTable pm = null;
		if (_params.get() != null) {
			pm = (ParamsTable) _params.get();
		}
		else {
			synchronized (this) {
				pm = new ParamsTable();
				Map m = getContext().getParameters();
				Iterator iter = m.keySet().iterator();
				while (iter.hasNext()) {
					String name = (String) iter.next();
					Object obj = m.get(name);
					if (obj instanceof String[] && ((String[]) obj).length > 0) {
						String tmp[] = (String[]) obj;
						pm.setParameter(name, tmp[0]);
					}
					else {
						pm.setParameter(name, obj + "");
					}
				}
			}
		}
		return pm;
	}

}
